package cn.cdeden.system.recycle;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.api.domain.RemoteEnterpriseFilesTreeService;
import cn.cdeden.knowledge.api.domain.vo.EnterpriseFilesTreeRemoteVo;
import cn.cdeden.system.api.model.LoginUser;
import cn.cdeden.system.domain.FilesTreeRecycle;
import cn.cdeden.system.domain.enums.TreeTypeEnum;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequiredArgsConstructor
@Component("enterpriseRecycle")
public class RecycleEnterprise implements RecycleInterface{

    @DubboReference(check = false)
    private final RemoteEnterpriseFilesTreeService remoteEnterpriseFilesTreeService;



    @Override
    public R foxBaseRecoverFileChooseAPath(List<FilesTreeRecycle> recycleList) {
        try {
            LoginUser user = LoginHelper.getLoginUser();
//            FileUserDto user = FileUserUtils.getUser(); //用户信息
            String username = user.getUsername();
            String userId = Long.toString(user.getUserId());
            List<EnterpriseFilesTreeRemoteVo> filesTrees = new ArrayList<>();
//            List<EnterpriseFilesTree> filesTrees = new ArrayList<>();
            recycleList.forEach(tree -> {
//            getFileTree(tree)
//            filesTrees.add();
                String parentId = tree.getParentId();
                EnterpriseFilesTreeRemoteVo byId = remoteEnterpriseFilesTreeService.getById(parentId);
//                EnterpriseFilesTree byId = enterpriseFilesTreeService.getById(parentId);
                if (ObjectUtils.isEmpty(byId)) {
                    throw new RuntimeException();
                }
                EnterpriseFilesTreeRemoteVo filesTree = MapstructUtils.convert(tree, EnterpriseFilesTreeRemoteVo.class);
                filesTree.setId(tree.getFileTreeId());
                filesTree.setSuperiorsPath(byId.getPath());
                filesTree.setDeleteBy(null);
                filesTree.setDeleteType(null);
                filesTree.setDeleteTime(null);
                filesTree.setDeleteId(null);

                filesTree.setUpdateTime(new Date());
                filesTree.setUpdateId(userId);
                filesTree.setUpdateBy(username);


                //是否重命名处理
                if (remoteEnterpriseFilesTreeService.nameRechecking(filesTree.getRealName(), parentId, filesTree.getSuffix()) != null) {
                    filesTree.setRealName(filesTree.getRealName() + "_恢复_" + System.currentTimeMillis());
                } else {
                    filesTree.setRealName(filesTree.getRealName() + "_恢复");
                }
                filesTree.setPath(byId.getPath() + "/" + filesTree.getRealName());
                //
                filesTrees.add(filesTree);

                if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())) {
                    List<FilesTreeRecycle> list = tree.getSonDataList();
                    if (CollectionUtils.isNotEmpty(list)) {
                        list.forEach(recycle1 -> {
                            List<EnterpriseFilesTreeRemoteVo> fileTree = getFileTree(recycle1, filesTree.getPath(), userId, username);
                            filesTrees.addAll(fileTree);
                        });
                    }
                }
            });
            if (CollectionUtils.isEmpty(filesTrees)) {
                throw new RuntimeException();
            }

            return R.ok(remoteEnterpriseFilesTreeService.saveBatch(filesTrees));
//            return BaseRespResult.successResult(remoteEnterpriseFilesTreeService.saveBatch(filesTrees));
        } catch (Exception e) {
            return R.fail("还原失败：" + e);
//            return BaseRespResult.errorResult("还原失败：" + e);
        }
    }


    private List<EnterpriseFilesTreeRemoteVo> getFileTree(FilesTreeRecycle recycle, String superiorsPath, String userId, String username) {
        List<EnterpriseFilesTreeRemoteVo> filesTrees = new ArrayList<>();
        EnterpriseFilesTreeRemoteVo tree = MapstructUtils.convert(recycle, EnterpriseFilesTreeRemoteVo.class);
        tree.setId(recycle.getFileTreeId());
        tree.setSuperiorsPath(superiorsPath);
        tree.setPath(superiorsPath + "/" + tree.getRealName());

        tree.setDeleteBy(null);
        tree.setDeleteType(null);
        tree.setDeleteTime(null);
        tree.setDeleteId(null);

        tree.setUpdateTime(new Date());
        tree.setUpdateId(userId);
        tree.setUpdateBy(username);


        filesTrees.add(tree);
        if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())) {
            List<FilesTreeRecycle> list = recycle.getSonDataList();
            if (CollectionUtils.isNotEmpty(list)) {

                list.forEach(recycle1 -> {
                    List<EnterpriseFilesTreeRemoteVo> fileTree = getFileTree(recycle1, tree.getPath(), userId, username);
                    filesTrees.addAll(fileTree);
                });
//                tree.setSonDataList(filesTrees);
            }
        }
        return filesTrees;
    }

}
